﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COFE.Core.Logger
{
    #region Non-Generic version
    public class EventLogger : IEventLogger
    {
        #region Constructor

        #endregion

        #region Methods

        public virtual void DispatchEvent(object sender, EventArgs args) 
        {
            string userId =
                      COFEShared.CurrentUserProfile == null ?
                      COFEShared.AdminUserProfile.UserName :
                      COFEShared.CurrentUserProfile.UserName;

            lock (_logList)
            {                
                _logList.Add(_counter, new EventLog(_counter, DateTime.UtcNow,
                    sender == null ? "" : sender.ToString(), userId, args));
                _counter++;
            }
        }

        public IEnumerable<IEventLog> GetEventLog(DateTime startTimeUtc, DateTime endTimeUtc)
        {
            lock (_logList)
                return from l in _logList.Values
                       where l.UtcTime >= startTimeUtc && l.UtcTime <= endTimeUtc
                       select l;
        }

        public IEnumerable<IEventLog> GetEventLog(TimeSpan pastTime)
        {
            return GetEventLog(DateTime.UtcNow.Subtract(pastTime), DateTime.UtcNow);

        }

        public IEnumerable<IEventLog> GetEventLog(string sender)
        {
            lock (_logList)
                return from l in _logList.Values
                       where l.Sender == sender
                       select l;
        }

        #endregion

        #region Data

        static Int64 _counter = 1; //Shared by all loggers.
        Dictionary<Int64, EventLog> _logList = new Dictionary<Int64, EventLog>();

        #endregion

        #region Public Properties

        public IEventLog this[long id]
        {
            get { return (_logList.ContainsKey(id)) ? _logList[id] : null; }
        }

        #endregion


        
    }
    #endregion

    #region Generic Version
    public class EventLogger<T> : EventLogger, IEventLogger<T> where T : EventArgs
    {
        #region Constructor

        #endregion

        #region Methods        

        public void DispatchEvent(object sender, T args)
        {
            base.DispatchEvent(sender, args);
        }

        public new IEnumerable<IEventLog<T>> GetEventLog(DateTime startTimeUtc, DateTime endTimeUtc)
        {
            return from l in base.GetEventLog(startTimeUtc, endTimeUtc)
                   select l.ToGeneric<T>();
        }

        public new IEnumerable<IEventLog<T>> GetEventLog(string sender)
        {
            return from l in base.GetEventLog(sender)
                   select l.ToGeneric<T>();
        }

        public new IEnumerable<IEventLog<T>> GetEventLog(TimeSpan pastTime)
        {
            return from l in base.GetEventLog(pastTime)
                   select l.ToGeneric<T>();
        }

        #endregion

        #region Data
        

        #endregion

        #region Public Properties

        public new IEventLog<T> this[long id]
        {
            get { 
                var baseLog = base[id];
                return baseLog == null ? null : baseLog.ToGeneric<T>();
            }
        }

        #endregion



     
    }


    #endregion
}
